package com.polaris.live.dialog

import android.content.Context
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.common.ext.dp
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.manager.CoroutineScopeManager
import com.polaris.live.common.network.manualRequest
import com.polaris.live.databinding.DialogNewLiveAnchorAchieveBinding
import com.polaris.live.im.sfs.bean.live.payload.NewAnchorSubsidyPayloadBean
import com.polaris.live.net.liveService
import com.polaris.live.translation.R.string
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch

/**
 * NewLiveAnchorDialog
 *
 * @author Created by 半仙 on 2024/1/19/019 16:33
 */
class NewLiveAnchorDialog(
    context: Context,
    private val mPayload: NewAnchorSubsidyPayloadBean,
) : CenterPopupView(context) {

    private lateinit var mBinding: DialogNewLiveAnchorAchieveBinding
    override fun getImplLayoutId() = R.layout.dialog_new_live_anchor_achieve

    private var mNetJob: Job? = null

    override fun onCreate() {
        super.onCreate()
        mBinding = DialogNewLiveAnchorAchieveBinding.bind(popupImplView)
        initListener()
        mBinding.tvBeans.text = mPayload.coin.toString()

        when (mPayload.type) {
            TYPE_FIRST -> {
                mBinding.tvBottom.visibility = View.VISIBLE
            }

            TYPE_CENTER -> {
                val layout = mBinding.llCenter.layoutParams as ConstraintLayout.LayoutParams
                layout.topMargin = 14.dp
                mBinding.llCenter.layoutParams = layout
                mBinding.tvTitle.visibility = View.VISIBLE
                mBinding.tvTitle.text = StringUtils.getString(string.today_achieve, mPayload.timeName)
            }

            TYPE_LAST -> {
                mBinding.ivCenter.setImageResource(R.drawable.img_tanchuang_finish)
                mBinding.tvTitle.text = StringUtils.getString(string.so_good_get_all)
                mBinding.tvTitle.visibility = View.VISIBLE
                mBinding.tvLastText.visibility = View.VISIBLE
                mBinding.tvGetMore.text = StringUtils.getString(string.tomorrow_reward)
                val layout = mBinding.llCenter.layoutParams as ConstraintLayout.LayoutParams
                layout.topMargin = 14.dp
                mBinding.llCenter.layoutParams = layout
            }
        }
        if (!mPayload.tomorrowSubsidy) {
            mBinding.tvGetMore.visibility = View.GONE
        }
    }

    private fun initListener() {
        mBinding.ivClose.setOnSingleClickListener {
            dismiss()
        }

        mBinding.tvGetMore.setOnSingleClickListener {
            mNetJob = CoroutineScopeManager.mainScope.launch {
                try {
                    manualRequest({ liveService.subsidies() }, {
                        XPopup.Builder(context)
                            .isDestroyOnDismiss(true)
                            .asCustom(AwardProgressDialog(context, it))
                            .show()
                        dismiss()
                    }, {})
                } finally {
                    if (mNetJob == coroutineContext[Job]) {
                        mNetJob = null
                    }
                }
            }
        }
    }

    override fun onDismiss() {
        super.onDismiss()
        mNetJob?.cancel()
        mNetJob = null
    }

    companion object {

        const val TYPE_FIRST = 1        //首次
        const val TYPE_CENTER = 2       //中间每一次
        const val TYPE_LAST = 9         //最后一次
    }
}